import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, TemplateRef } from '@angular/core';
import { HelpTooltipModule } from './help-tooltip.module';
import { CommonModule } from '@angular/common';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { HelpTooltipService } from '../core/services/help-tooltip.service';

@Component({
  template: `
    <lv-help-tooltip [lvIcon]="lvIcon" [lvContent]="lvContent"></lv-help-tooltip>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
class TestHelpTooltipComponent {
  @Input() lvIcon: string = 'lv-icon-status-error';
  @Input() lvContent: string | TemplateRef<any>;
}

describe('HelpTooltipComponent', () => {
  let helpTooltipService: HelpTooltipService;
  beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [TestHelpTooltipComponent],
      imports: [HelpTooltipModule, CommonModule, NoopAnimationsModule],
      providers: [HelpTooltipService],
      teardown: { destroyAfterEach: false },
    }).compileComponents();
    helpTooltipService = TestBed.inject(HelpTooltipService);
  }));

  describe('render', () => {
    let component: TestHelpTooltipComponent;
    let fixture: ComponentFixture<TestHelpTooltipComponent>;
    let cdr: ChangeDetectorRef;

    const detectChange = () => {
      cdr.detectChanges();
      fixture.detectChanges();
      tick(500);
    };

    beforeEach(fakeAsync(() => {
      fixture = TestBed.createComponent(TestHelpTooltipComponent);
      component = fixture.componentInstance;
      cdr = fixture.debugElement.injector.get(ChangeDetectorRef);
      component.lvContent = 'test';
      component.lvIcon = 'lv-icon-status-error';
      detectChange();
    }));

    it('render correctly', fakeAsync(() => {
      expect(fixture).toMatchSnapshot();
      component.lvIcon = 'lv-icon-status-error';
      detectChange();
      expect(fixture).toMatchSnapshot();
    }));

    it('function', fakeAsync(() => {
      helpTooltipService.status.next({});
      detectChange();
      expect(fixture).toMatchSnapshot();
    }));
  });
});
